//
//  MJQuizzesView.m
//  MJ
//
//  Created by lina on 2017/12/7.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJQuizzesView.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "MJHomeModel.h"
#import <YYKit.h>

@interface MJQuizzesView ()

/** imgView */
@property (nonatomic, strong) UIImageView *bgImg;

/** label */
@property (nonatomic, strong) UILabel *titleLabel;

/** contentScrol */
@property (nonatomic, strong) UIScrollView *contentScrol;

/** content */
@property (nonatomic, strong) UILabel *contentLabel;


@end

@implementation MJQuizzesView

- (instancetype)init{
    if (self = [super init]) {
        
        [self creatView];
    }
    return self;
}

- (void)creatView {
    
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 40/2;
    
    
    _bgImg = [[UIImageView alloc] init];
    _bgImg.layer.masksToBounds = YES;
    [self addSubview:_bgImg];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = HEXColor(@"#323232");
    _titleLabel.numberOfLines = 0;
    _titleLabel.font = [UIFont systemFontOfSize:20];
    [self addSubview:_titleLabel];
    
    _contentScrol = [[UIScrollView alloc] init];
    _contentScrol.showsHorizontalScrollIndicator = NO;
    [self addSubview:_contentScrol];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.textColor = HEXColor(@"#8c8c8c");
    _contentLabel.font = [UIFont systemFontOfSize:14];
    _contentLabel.numberOfLines = 0;
    [_contentScrol addSubview:_contentLabel];
    
    _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_startButton setBackgroundColor:HEXColor(@"#ff8280")];
    [_startButton setTitle:@"开始测试" forState:UIControlStateNormal];
    [_startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _startButton.layer.masksToBounds = YES;
    _startButton.layer.cornerRadius = 45/2;
    
    [self addSubview:_startButton];
    
    
    [_bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.height.mas_equalTo((kScreenWidth-40)/650*398);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(20);
        make.right.mas_equalTo(self).offset(-20+5);
        make.top.mas_equalTo(_bgImg.mas_bottom).offset(20);
    }];
    
    [_contentScrol mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLabel.mas_bottom).offset(15);
        make.left.right.mas_equalTo(_titleLabel);
        make.bottom.mas_equalTo(_startButton.mas_top).offset(-20);
    }];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_contentScrol).offset(0);
        make.left.right.mas_equalTo(_titleLabel);
    }];
    
    [_startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(200, 45));
        make.centerX.mas_equalTo(self);
        if (kScreenHeight ==  568) {
            make.bottom.mas_equalTo(self).offset(-20);
        }else{
            make.bottom.mas_equalTo(self).offset(-30);
        }
    }];
}

- (void)setQuizzes:(Quizzes *)quizzes{
    [_bgImg sd_setImageWithURL:[NSURL URLWithString:quizzes.pic]];
    _titleLabel.text = quizzes.title;
    _contentLabel.text = quizzes.intro;
    _contentScrol.contentSize = CGSizeMake(kScreenWidth-100, [self sizeWithText:quizzes.intro font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(kScreenWidth-80, MAXFLOAT)].height);
}


//计算文字的长度
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
}

@end
